package com.dhl.fin.api.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.dhl.fin.api.common.dto.*;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.common.util.JsonUtil;
import com.dhl.fin.api.common.util.SpringContextUtil;
import com.dhl.fin.api.dto.WeChatPayRiseResponseDTO;
import com.dhl.fin.api.dto.WeXinPayCallBackDTO;
import com.dhl.fin.api.dto.WeiXinCreatePayOrderDTO;
import com.dhl.fin.api.dto.WeiXinRefundDTO;
import com.dhl.fin.api.service.WeChatServiceImpl;
import com.google.zxing.WriterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 微信支付
 */
@Slf4j
@RestController
@RequestMapping("weixin")
public class PayController {

    @Autowired
    private WeChatServiceImpl weChatService;


    /**
     * @param createPayOrderDTO 发起微信支付订单
     * @return
     * @throws IOException
     */
    @PostMapping("/pay")
    public ApiResponse pay(@RequestBody @Validated WeiXinCreatePayOrderDTO createPayOrderDTO) throws IOException, WriterException {
        String dateStr = DateUtil.getTime(DateUtil.getSysDate()).replaceAll("-", "");
        String comDateNum = StrUtil.padPre(createPayOrderDTO.getOrderNum().toString(), 4, '0');
        String outTradeNo = IdUtil.simpleUUID().substring(0, 10).concat("_").concat(dateStr).concat(comDateNum);
        String createCallBackUrl = SpringContextUtil.getPropertiesValue("wechat.createCallBackUrl");
        WeChatPayRiseResponseDTO weChatPayRiseResponseDTO = weChatService.riseOrder(createPayOrderDTO.getPrePrice(),
                createPayOrderDTO.getDescription(), outTradeNo,
                createPayOrderDTO.getExpireDateTime(),
                createCallBackUrl
        );
        return ApiResponse.success(weChatPayRiseResponseDTO);
    }

    /**
     * @param refundDTO 退款信息
     * @return
     */
    @PostMapping("/refund")
    public ApiResponse refund(@RequestBody @Validated WeiXinRefundDTO refundDTO) {
        log.info("发起退款", JsonUtil.objectToString(refundDTO));
        weChatService.refundOrder(refundDTO);
        return ApiResponse.success();
    }


    /**
     * 解密
     *
     * @param
     * @return
     */
    @PostMapping("/decrypt")
    public ApiResponse decrypt(@RequestBody WeXinPayCallBackDTO weXinPayCallBackDTO) {
        try {
            return ApiResponse.success(weChatService.decryptCiphertext(weXinPayCallBackDTO.getCiphertext(), weXinPayCallBackDTO.getNonce(), weXinPayCallBackDTO.getAssociatedData()));
        } catch (Exception e) {
            log.error("密文解密失败，{}", JsonUtil.objectToString(weXinPayCallBackDTO), e);
            return ApiResponse.error("解密失败");
        }
    }


}




